#include <stdio.h>
#include <sqlite3.h>
#include <time.h>
int sqlite_func();
int sqil_func(char *p);
int myfunc(void *p,int argc,char**argv,char**argvv)
{
	int i;
	*(int*)p=0;
	for(i=0;i<argc;i++)
	{
		printf("%s=%s",argvv[i],argv[i]? argv[i]:"NULL");
	}
	printf("\n");
	return 0;
}

int main()
{int ret=0;
	time_t timep;
	time(&timep);
	struct tm *p=gmtime(&timep);
printf("Content-Type: text/html \n\n");
 printf("<html>\n");
printf("<head>\n");
printf("<title>\n");
printf("LED灯\n");
printf("</title>\n");
printf("</head>\n");
 printf("<body>\n");
 printf("<style type=\"text/css\">");
//printf("")
 printf("</style>");
 printf("<div class=\"1\">");
 printf("<center><h1>智慧农场环境检测系统</h1></center>\n");
 ret=sqlite_func();
 if(ret!=-1){
	 printf("<center><table bgcolor=\"#0ebfaa\">\n");
	 printf("<tr>\n");
	 printf("<td><font size=\"5\">LED灯状态：</font></td>\n");
	 printf("<center><td align =\"center\"><font size=\"5\">LED灯开启成功</font></td></center>\n");
	 printf("</tr>\n");
	 printf("<tr><td align=\"center\"><font size=\"5\">时     间:</font></td>\n");
	 printf("<td><font size=\"5\">\n");
	 printf("%d/%d/%d", (1900+p->tm_year), (1+p->tm_mon), p->tm_mday);
	 printf(" %d:%d:%d\n", (8+p->tm_hour), p->tm_min, p->tm_sec);
	 printf("</td></font></tr>\n");

	 printf("<tr align=\"center\">\n");
	 printf("<td><button type =\"button\" onclick=\'location.href=(\"http://47.98.123.181/cgi-bin/w_m4.cgi\")\'><font size=\"5\"> 关灯</font></button></td>");
 }
 else
	 printf("<center><h2>LED灯开启失败！\n</h2></center>");
 printf("<td align=\"center\"><button type =\"button\" onclick=\'location.href=(\"http://47.98.123.181/cgi-bin/w_m.cgi\")\'> <font size=\"5\">返回</font></button></td></tr>");
printf("</table></center>\n");
 printf("</div>\n");
 printf("</body>\n");
 printf("</html>\n");
 return 0;
}


int sqlite_func()
{
	sqlite3 *db;
	int nrow = 0, ncolumn = 0;
	char **azResult = NULL;
	char *zErrMsg = 0;
	char sql[500];
	char *errmsg=0;
	char empty=1;
	int rc = 0;
	rc = sqlite3_open("/var/www/xuedao", &db);
	if (rc)
	{
		printf("Content-type: text/html\n\n");
		fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
		sqlite3_close(db);
		//return sql_errror();
	}

	rc=sqlite3_exec(db,"delete from LED;",myfunc,&empty,&errmsg);
	if(rc!=SQLITE_OK){
		printf("delete error\n");
	}

	sprintf(sql, "insert into LED values(1);");
	rc = sqlite3_exec(db ,sql , myfunc , &empty ,&errmsg);
	if (rc != SQLITE_OK)
	{
		printf("Content-type: text/htmlggg\n\n");
		fprintf(stderr, "SQL error: %s\n", errmsg);
		sqlite3_close(db);
		return -1;
	}

}

